import 'package:base_lib/common/base/base_bloc_page.dart';
import 'package:base_lib/common/widget/peach_button.dart';
import 'package:base_lib/router/index.dart';
import 'package:base_lib/theme/theme_global.dart';
import 'package:base_lib/utils/util.dart';
import 'package:flutter/material.dart';
import 'package:fsuper/fsuper.dart';
import 'bloc.dart';

///设置登录密码
class SetLoginPasswordPage
    extends BaseBlocPage<SetLoginPasswordBloc, _SetLoginPasswordPageState> {
  SetLoginPasswordPage(Parameters parameters) : super(parameters);

  @override
  _SetLoginPasswordPageState generateThemeState() {
    return _SetLoginPasswordPageState();
  }
}

class _SetLoginPasswordPageState
    extends BaseBlocPageState<SetLoginPasswordBloc> {
  @override
  Widget buildChild(BuildContext context, MTheme theme) {
    return Material(
      child: Scaffold(
          appBar: AppBar(
            leading: BackButton(
              onPressed: () {
                Routers.pop(context);
              },
            ),
            title: Text("设置登录密码"),
            centerTitle: true,
          ),
          body: Container(
            color: Colors.white,
            height: double.infinity,
            width: double.infinity,
            padding: EdgeInsets.symmetric(vertical: 10, horizontal: 28),
            child: SingleChildScrollView(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text(
                    "设置登录密码",
                    style: TextStyle(
                        color: Colors.black,
                        fontWeight: FontWeight.w500,
                        fontSize: 30),
                  ),
                  Padding(
                    padding: EdgeInsets.only(top: 60),
                    child: Container(),
                  ),
                  Container(
                      width: double.infinity,
                      margin: EdgeInsets.only(top: 30),
                      child: _buildPwd(theme)),
                  Container(width: double.infinity, child: _buildRePwd(theme)),
                  StreamBuilder(
                    stream: bloc.loadingStatus,
                    initialData: false,
                    builder: (context, AsyncSnapshot<bool> loading) {
                      return Padding(
                        padding: const EdgeInsets.only(top: 30.0),
                        child: peachButtonBuilder(
                          onTap: () {
                            if (!loading.data)
                              bloc.setPwd(
                                  context, parameters.getString("phone"));
                          },
                          text: loading.data ? "请求中..." : "设置密码",
                        ),
                      );
                    },
                  ),
                ],
              ),
            ),
          )),
    );
  }

  @override
  SetLoginPasswordBloc generateBloc() {
    return SetLoginPasswordBloc();
  }

  @override
  ignoreWindowWidget() => true;

  ///密码输入框
  Widget _buildPwd(MTheme theme) {
    return Column(
      children: [
        TextField(
          keyboardType: TextInputType.visiblePassword,
          obscureText: false,
          maxLength: 20,
          onChanged: (value) {
            bloc.editPwdChanged(value);
          },
          cursorColor: Colors.grey,
          strutStyle:
              StrutStyle(fontSize: theme.themeFontSize.fontSizeBig, height: 1),
          style: TextStyle(
              color: Colors.black, fontSize: theme.themeFontSize.fontSizeBig),
          decoration: InputDecoration(
            hintText: "请输入密码",
            counterText: "",
            hintStyle: TextStyle(
                color: theme.themeColor.themeLightGreyColor, fontSize: 18),
            border: InputBorder.none,
          ),
        ),
        Divider(
          height: 10,
          color: theme.themeColor.themeLightGreyColor,
        )
      ],
    );
  }

  ///确认密码输入框
  Widget _buildRePwd(MTheme theme) {
    return Column(
      children: [
        TextField(
          keyboardType: TextInputType.visiblePassword,
          obscureText: false,
          maxLength: 20,
          onChanged: (value) {
            bloc.editRePwdChanged(value);
          },
          cursorColor: Colors.grey,
          strutStyle:
              StrutStyle(fontSize: theme.themeFontSize.fontSizeBig, height: 1),
          style: TextStyle(
              color: Colors.black, fontSize: theme.themeFontSize.fontSizeBig),
          decoration: InputDecoration(
            hintText: "请再次输入密码",
            counterText: "",
            hintStyle: TextStyle(
                color: theme.themeColor.themeLightGreyColor, fontSize: 18),
            border: InputBorder.none,
          ),
        ),
        Divider(
          height: 10,
          color: theme.themeColor.themeLightGreyColor,
        )
      ],
    );
  }
}
